<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
        <script>
			var data = [["java", 10000], ["html5", 200], ["javascript", 50000]];
			function Book(title, price) {
				this.title = title;
				this.price = price;
				this.toString = function() {
					return "title : " + this.title + ", price : " + this.price;
				};
			}

			var bookObjs = [];
			
			function createObject() {
				for (var row in data) {
					bookObjs.push(new Book(data[row][0], data[row][1]));
				}
			}

			createObject();
			console.log("original bookObjs : " + bookObjs);
			
			var sortByPrice1 = function(x, y) {
				return x.price > y.price;
			};
			var sortByPrice2 = function(x, y) {
				return y.price > x.price;
			};
			var sortByName1 = function(x, y) {
				return x.title > y.title;

			};
			var sortByName2 = function(x, y) {
				return y.title > x.title;

			};
			function sortandprintArray(x, y) {
				console.log("sort and print ");
				x.sort(y);
				console.log("sorted bookObjs :" + x);
			}

			var type;
			while (type != 5) {
				var type = prompt("sort 방법을 선택하세요.", "1. 가격(오름), 2. 가격(내림), 3. 제목(오름), 4. 제목(내림), 5. 그만");
				console.log(type + (type == 1));
				if (type == 1) {
					sortandprintArray(bookObjs, sortByPrice1);
				}
				switch(type) {
				case "1":
					sortandprintArray(bookObjs, sortByPrice1);
					break;
				case "2":
					sortandprintArray(bookObjs, sortByPrice2);
					break;
				case "3":
					sortandprintArray(bookObjs, sortByName1);
					break;
				case "4":
					sortandprintArray(bookObjs, sortByName2);
					break;
				default:
					console.log("unknown type " + type);
					break;
				}
				console.log("work over.");
			}
        </script>
    </head>
    <body>

    </body>
</html>